package edu.ccut.saturn.component.impl.sql;

import java.util.Map;

import edu.ccut.saturn.component.ISaturnDataSqlItemInfo;
import edu.ccut.saturn.component.xml.ISaturnElement;

public abstract class AbstractSingleExprSqlItemInfo extends AbstractSqlItemInfo {

	private String property;

	public String getProperty() {
		return this.property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	protected String parseSql(Map<String, Object> param) throws Exception {
		StringBuffer text = new StringBuffer();

		if (getSaturnDataSqlItemInfos() != null
				&& getSaturnDataSqlItemInfos().size() > 0) {

			for (ISaturnDataSqlItemInfo sqlItemInfo : getSaturnDataSqlItemInfos()) {
				text.append(" " + sqlItemInfo.getSubSql(param) + " ");
			}
		}

		if (this.getPrepend() != null && !"".equals(this.getPrepend())) {
			text.insert(0, this.getPrepend() + " ");
		}
		return text.toString();
	}

	public AbstractSingleExprSqlItemInfo(String prepend, String property) {

		super(prepend);
		this.property = property;
	}

	@Override
	public ISaturnElement toSaturnElement() {
		ISaturnElement element = super.toSaturnElement();
		element.addAttribute("property", this.property);
		return element;
	}
}
